package view;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import model.Book;
import dao.BookDAO;

public class AddBookDialog extends JDialog {
    private JTextField titleField;
    private JTextField authorField;
    private JTextField priceField;
    private JTextField stockField;
    private BookPanel bookPanel;
    
    public AddBookDialog(JFrame parent, BookPanel bookPanel) {
        super(parent, "添加新图书", true);
        this.bookPanel = bookPanel;
        
        setLayout(new GridLayout(5, 2, 5, 5));
        
        // 添加输入框
        add(new JLabel("书名："));
        titleField = new JTextField();
        add(titleField);
        
        add(new JLabel("作者："));
        authorField = new JTextField();
        add(authorField);
        
        add(new JLabel("价格："));
        priceField = new JTextField();
        add(priceField);
        
        add(new JLabel("库存："));
        stockField = new JTextField();
        add(stockField);
        
        // 添加按钮
        JButton saveBtn = new JButton("保存");
        JButton cancelBtn = new JButton("取消");
        
        saveBtn.addActionListener(e -> saveBook());
        cancelBtn.addActionListener(e -> dispose());
        
        add(saveBtn);
        add(cancelBtn);
        
        styleComponents();
        
        pack();
        setLocationRelativeTo(parent);
    }
    
    private void saveBook() {
        try {
            String title = titleField.getText();
            String author = authorField.getText();
            double price = Double.parseDouble(priceField.getText());
            int stock = Integer.parseInt(stockField.getText());
            
            Book book = new Book(0, title, author, price, stock);
            BookDAO dao = new BookDAO();
            
            if (dao.addBook(book)) {
                JOptionPane.showMessageDialog(this, "添加成功！");
                bookPanel.refreshTable();
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, "添加失败！", "错误", JOptionPane.ERROR_MESSAGE);
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "请输入有效的数字！", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void styleComponents() {
        // 设置字体
        Font labelFont = new Font("微软雅黑", Font.PLAIN, 14);
        Font inputFont = new Font("微软雅黑", Font.PLAIN, 14);
        
        // 设置输入框样式
        titleField.setFont(inputFont);
        authorField.setFont(inputFont);
        priceField.setFont(inputFont);
        stockField.setFont(inputFont);
        
        // 设置标签样式
        for (Component comp : getContentPane().getComponents()) {
            if (comp instanceof JLabel) {
                JLabel label = (JLabel) comp;
                label.setFont(labelFont);
            }
        }
        
        // 设置按钮样式
        for (Component comp : getContentPane().getComponents()) {
            if (comp instanceof JButton) {
                JButton button = (JButton) comp;
                button.setFont(labelFont);
                button.setFocusPainted(false);
                button.setBackground(new Color(59, 89, 152));
                button.setForeground(Color.WHITE);
                button.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 15));
            }
        }
        
        // 设置输入框边框
        Border border = BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(204, 204, 204)),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)
        );
        titleField.setBorder(border);
        authorField.setBorder(border);
        priceField.setBorder(border);
        stockField.setBorder(border);
    }
} 